<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable filterable >
          <el-option v-for="dict in supplierIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable

          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="退货日期">
        <el-date-picker v-model="daterangeReturnDate"  style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"   @click="handleQuery">搜索</el-button>
        <el-button    @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button  type="warning" plain   @click="handleExport">导出</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="returnList">
      <el-table-column label="物料名称" align="center" prop="materialName"  width="200" />
      <el-table-column label="生产批号" align="center" prop="batchNo"  width="200" />
      <el-table-column label="供应商" align="center" prop="supplierName"  width="200" />
      <el-table-column label="退货单号" align="center" prop="returnNo"  width="200" />
      <el-table-column label="型号" align="center" prop="materialModel"  width="200" />
      <el-table-column label="规格" align="center" prop="materialSpecification"  width="200" />
      <el-table-column label="单位" align="center" prop="materialUnit"  width="200" />
      <el-table-column label="单价" align="center" prop="price"  width="200" />
      <el-table-column label="订货数量" align="center" prop="bookQuantity"  width="200" />
      <el-table-column label="到货数量" align="center" prop="arriveQuantity"  width="200" />
      <el-table-column label="退货数量" align="center" prop="returnQuantity"  width="200" />
      <el-table-column label="退货日期" align="center" prop="returnDate"  width="200" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import {
    listReturn,
    exportReturn
  } from "@/api/purchaseReport/return";

  export default {
    name: "Return",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 采购退货统计表格数据
        returnList: [],
        // 供应商id字典
        supplierIdOptions: [],
        // 退货日期时间范围
        daterangeReturnDate: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          materialId: null,
          batchNo: null,
          materialName: null,
          bookQuantity: null,
          arriveQuantity: null,
          supplierId: null,
          returnDate: null,
          supplierName: null,
          returnNo: null
        },
      };
    },
    created() {
      this.getList();
      this.getQueryData("query_supplier").then(res => {
        this.supplierIdOptions = res.data;
      });
    },
    methods: {
      /** 查询采购退货统计列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeReturnDate && '' != this.daterangeReturnDate) {
          this.queryParams.params["beginReturnDate"] = this.daterangeReturnDate[0];
          this.queryParams.params["endReturnDate"] = this.daterangeReturnDate[1];
        }
        listReturn(this.queryParams).then(response => {
          this.returnList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 供应商id字典翻译
      supplierIdFormat(row, column) {
        return this.selectDictLabel(this.supplierIdOptions, row.supplierId);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeReturnDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有采购退货统计数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportReturn(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }

  };

</script>
